package com.wangsd.web.coupon.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.enums.ResultCode;
import com.wangsd.web.MyController;
import com.wangsd.web.coupon.entity.Company;
import com.wangsd.web.coupon.entity.FileManagement;
import com.wangsd.web.coupon.entity.Merchant;
import com.wangsd.web.coupon.service.CompanyService;
import com.wangsd.web.coupon.service.MerchantService;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

import static com.wangsd.common.enums.ResultCode.PRARM_NOT_NULL;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-19
 */
@Api(value = "company", tags = "商圈（公司）管理")
@RestController
@RequestMapping("/coupon/company")
public class CompanyController extends MyController {

    @Autowired
    CompanyService companyService;

    @Autowired
    MerchantService merchantService;

    @Autowired
    SysUserService sysUserService;

    @ApiOperation(value = "查询商圈（公司）列表", notes = "查询商圈（公司）列表", httpMethod = "GET")
    @PostMapping("/selectList/{pageNum}/{pageSize}")
    public Result selectList(@ApiParam(value = "当前页", required = true) @PathVariable Integer pageNum,
                             @ApiParam(value = "每页大小", required = true) @PathVariable Integer pageSize,
                             @RequestBody @ApiParam(name = "company", value = "传入json格式") Company company) {
        SysUser userInfo = getUserInfo();
        IPage<Company> companyIPage = companyService.selectPage(userInfo.getCode(), pageNum, pageSize, company);
        return Result.success(companyIPage);

    }

    @ApiOperation(value = "删除商圈（公司）", notes = "删除商圈（公司）", httpMethod = "GET", response = Result.class)
    @GetMapping("/delete/{id}")
    public Result delete(@ApiParam(value = "商圈（公司）id", required = true) @PathVariable Long id) {
        List<Merchant> merchantList = merchantService.list(new QueryWrapper<Merchant>().eq("company_id", id));
        if (merchantList != null && merchantList.size() > 0) {
            return Result.fail("请先删除当前商圈下的商户");
        }
        List<SysUser> sysUsers = sysUserService.list(new QueryWrapper<SysUser>().eq("parent_id", id).eq("type", 2));
        if(sysUsers!=null&&sysUsers.size()>0){
            return Result.fail("请先删除商圈下的商圈用户！");
        }
        boolean b = companyService.removeById(id);
        return Result.success(b);
    }

    @ApiOperation(value = "新增或修改商圈（公司）信息", notes = "新增或修改商圈（公司）信息", httpMethod = "POST", response = Result.class)
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Company company) {

        if (company.getBusinessServiceId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "服务商" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(company.getName())) {
            return new Result(PRARM_NOT_NULL.getCode(), "名称" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(company.getAddress())) {
            return new Result(PRARM_NOT_NULL.getCode(), "地址" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(company.getRealName())) {
            return new Result(PRARM_NOT_NULL.getCode(), "公司名称" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(company.getPhone())) {
            return new Result(PRARM_NOT_NULL.getCode(), "电话" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(company.getMiniAppid())) {
            return new Result(PRARM_NOT_NULL.getCode(), "miniAppid" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(company.getMiniSecret())) {
            return new Result(PRARM_NOT_NULL.getCode(), "miniSecret" + PRARM_NOT_NULL.getMessage());
        }

        Company repeatCompany = companyService.selectRepeat(company.getName(), company.getId());
        if (repeatCompany != null) {
            return Result.message(ResultCode.NAME_REPEAT);
        }
        companyService.addOrUpdate(company);
        return Result.success();

    }


    @ApiOperation(value = "新增或修改商圈（公司）信息", notes = "新增或修改商圈（公司）信息", httpMethod = "POST", response = Result.class)
    @PostMapping("/addFileConfig")
    public Result addFileConfig(@RequestBody FileManagement fileManagement) {

        if (fileManagement.getMasterId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "masterId" + PRARM_NOT_NULL.getMessage());
        }

        /*if (fileManagement.getType() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "type" + PRARM_NOT_NULL.getMessage());
        }*/

        if (fileManagement.getFileManagementList() == null || fileManagement.getFileManagementList().size() != 4) {
            return new Result(PRARM_NOT_NULL.getCode(), "轮播图3张必传，背景图1张必传");
        }

        companyService.addFileConfig(fileManagement);
        return Result.success();

    }

    @ApiOperation(value = "查询所有商圈（公司）", notes = "查询所有商圈（公司）", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public Result selectAll() {
        SysUser loginUser = getUserInfo();
        List<Map> list = companyService.selectAllByUserCode(loginUser.getCode());
        return Result.success(list);
    }

}

